package cn.cqautotest.cqviehelper.views;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.ColorInt;
import androidx.annotation.Nullable;

import cn.cqautotest.cqviehelper.R;

public class TitleBarView extends LinearLayout {

    private boolean isShowing = false;
    private ImageView mIvScan;
    private ImageView mIvAdd;
    private TextView mTvWeek;
    private ImageView mIvFold;
    private TextView mTvSemester;
    private View mWeekParent;
    private OnItemClickBaseListener mOnItemClickBaseListener = null;
    private OnItemLongClickBaseListener mOnItemLongClickBaseListener = null;

    public TitleBarView(Context context) {
        this(context, null);
    }

    public TitleBarView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TitleBarView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
        initEvent();
    }

    private void initEvent() {
        mIvScan.setOnClickListener(v -> {
            if (mOnItemClickBaseListener != null) {
                mOnItemClickBaseListener.onScannerClick();
            }
        });
        //如果图标被点击了，则触发 mWeekParent 的点击事件
        mIvFold.setOnClickListener(v -> {
            setShowing(!isShowing);
            if (mOnItemClickBaseListener != null) {
                mOnItemClickBaseListener.onChangeWeekClick(isShowing);
            }
        });
        mWeekParent.setOnClickListener(v -> {
            setShowing(!isShowing);
            if (mOnItemClickBaseListener != null) {
                mOnItemClickBaseListener.onChangeWeekClick(isShowing);
            }
        });
        mIvAdd.setOnClickListener(v -> {
            if (mOnItemClickBaseListener != null) {
                mOnItemClickBaseListener.onAddClick();
            }
        });
        mIvScan.setOnLongClickListener(v -> {
            if (mOnItemLongClickBaseListener != null) {
                return mOnItemLongClickBaseListener.onScannerLongClick();
            }
            return false;
        });
        mWeekParent.setOnLongClickListener(v -> {
            if (mOnItemLongClickBaseListener != null) {
                return mOnItemLongClickBaseListener.onChangeWeekLongClick(isShowing);
            }
            return false;
        });
        mIvAdd.setOnLongClickListener(v -> {
            if (mOnItemLongClickBaseListener != null) {
                return mOnItemLongClickBaseListener.onAddLongClick();
            }
            return false;
        });
    }

    private void initView() {
        LayoutInflater.from(getContext()).inflate(R.layout.view_bar_title, this);
        mIvScan = findViewById(R.id.ivScan);
        mWeekParent = findViewById(R.id.weekParent);
        mTvWeek = findViewById(R.id.tvWeek);
        mIvFold = findViewById(R.id.ivFold);
        mTvSemester = findViewById(R.id.tvSemester);
        mIvAdd = findViewById(R.id.ivAdd);
    }

    public void setOnItemClickBaseListener(OnItemClickBaseListener onItemClickBaseListener) {
        mOnItemClickBaseListener = onItemClickBaseListener;
    }

    public void setOnItemLongClickBaseListener(OnItemLongClickBaseListener onItemLongClickBaseListener) {
        mOnItemLongClickBaseListener = onItemLongClickBaseListener;
    }

    /**
     * 设置第几周
     *
     * @param week 周次
     * @return
     */
    public TitleBarView setWeek(int week) {
        setWeekText(String.format("第%s周", week));
        return this;
    }

    /**
     * 设置周次的文字
     *
     * @param text 要显示的文字
     * @return
     */
    public TitleBarView setWeekText(CharSequence text) {
        setWeekText(text, 0);
        return this;
    }

    /**
     * 设置周次文字的同时设置颜色
     *
     * @param text
     * @param color
     * @return
     */
    public TitleBarView setWeekText(CharSequence text, @ColorInt int color) {
        mTvWeek.setText(text);
        if (color != 0) setWeekTextColor(color);
        return this;
    }

    public TitleBarView setWeekTextColor(@ColorInt int color) {
        mTvWeek.setTextColor(color);
        return this;
    }

    public TitleBarView setTermText(CharSequence text) {
        mTvSemester.setText(text);
        return this;
    }

    public TextView getTvWeek() {
        return mTvWeek;
    }

    public TextView getTvSemester() {
        return mTvSemester;
    }

    public void setShowing(boolean showing) {
        isShowing = showing;
        mIvFold.setImageResource(isShowing ? R.mipmap.img_fold : R.mipmap.img_unfold);
    }

    public boolean isShowing() {
        return isShowing;
    }

    public interface OnItemClickBaseListener {
        void onScannerClick();

        void onChangeWeekClick(boolean isShowing);

        void onAddClick();
    }

    public static abstract class OnItemClickListener implements OnItemClickBaseListener {

        @Override
        public void onScannerClick() {

        }

        @Override
        public void onChangeWeekClick(boolean isShowing) {

        }

        @Override
        public void onAddClick() {

        }
    }

    public interface OnItemLongClickBaseListener {
        boolean onScannerLongClick();

        boolean onChangeWeekLongClick(boolean isShowing);

        boolean onAddLongClick();
    }

    public static class OnItemLongClickListener implements OnItemLongClickBaseListener {

        @Override
        public boolean onScannerLongClick() {
            return false;
        }

        @Override
        public boolean onChangeWeekLongClick(boolean isShowing) {
            return false;
        }

        @Override
        public boolean onAddLongClick() {
            return false;
        }
    }
}
